<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller {
    public function index(){    	
		$this->display();
    }
    //验证码
    public function verify(){
    	$config = array(    
    	'fontSize'    =>    15,    // 验证码字体大小 
    	'length'      =>    4,     // 验证码位数        	
    	);
    	$Verify = new \Think\Verify($config);                        
        $Verify->entry();			
    }
    
    public function dologin(){     	
    	if(!$_POST) halt('页面不能存在'); 	//防止注入，需要post传递过来的数据    	
    	$name = $_POST['username'];
    	$pwd = md5($_POST['password']);
    	$code = $_POST['code'];
    	$Verify= new \Think\Verify();
    	$Verify->reset = false;    // 验证成功后是否重置，设置为不重置可以二次验证。
    	if(!$Verify->check($code)){
    		$this->error('验证码有误');
    	}
    	$m = M('user');    	   
    	$where['name'] = $name;
    	$where['pwd'] = $pwd;    	    	
    	$sum = $m->where($where)->count();    //判断是否有数据
    	$user = $m->where(array('name'=>$name))->find();
    	echo $sum;
    	if($sum>0){
    		$data = array(
    			'id' => $user['id'],
    			'logintime' => time(),
    			'loginip' => get_client_ip()
    		);
    		$m->save($data);			//更新用户数据表
    		session('id',$user['id']);
    		session('name',$user['name']);
    		session('logintime', date('Y-m-d H:i:s',$user['logintime']));
    		session('loginip',$user['loginip']);		
    		$this->redirect('Index/index');
    	}else{
    		$this->error('用户名密码错误');
    	}    	
    }    
    public function quit(){
    	session_unset();
    	session_destroy();
    	$this->redirect('Login/index');
    }    
}